﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace projekty2
{
    public partial class frmVyhladavaniePrac : Form
    {
        public frmVyhladavaniePrac()
        {
            InitializeComponent();
        }

        private void frmVyhladavaniePrac_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dsVsetko.projekty' table. You can move, or remove it, as needed.
            this.projektyTableAdapter.Fill(this.dsVyhladavanie.projekty);
            this.uzivateliaTableAdapter.Fill(this.dsVyhladavanie.uzivatelia);

        }

        private void btnNajdi_Click(object sender, EventArgs e)
        {
            String text = "select praceId, prace.popis as popisPrace FROM prace, ulohy, projekty " +
                "WHERE prace.ulohyId = ulohy.ulohyId AND ulohy.projektyId = projekty.projektyId ";
            SqlConnection sqlConnection = new SqlConnection(global::projekty2.Properties.Settings.Default.connectionKlesako);
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            SqlDataReader sqlDataReader;
            if (chbProjekt.Checked)
            {
                text += " AND projekty.projektyId = @projektyId ";
                sqlCommand.Parameters.Add("@projektyId", SqlDbType.Int);
                sqlCommand.Parameters["@projektyId"].Value = cbxProjekt.SelectedValue; ;
            }
            if (chbUzivatel.Checked)
            {
                text += " AND ulohy.vypracovatelId = @vypracovatelId ";
                sqlCommand.Parameters.Add("@vypracovatelId", SqlDbType.Int);
                sqlCommand.Parameters["@vypracovatelId"].Value = cbxUzivatel.SelectedValue;
            }
            if (chbPopis.Checked)
            {
                text += " AND prace.popis LIKE @pracePopis ";
                sqlCommand.Parameters.Add("@pracePopis", SqlDbType.NVarChar);
                sqlCommand.Parameters["@pracePopis"].Value = "%" + txtPopis.Text + "%";
            }

            Console.WriteLine(text);
            sqlCommand.CommandText = text;
            sqlConnection.Open();
            sqlDataReader = sqlCommand.ExecuteReader();
            dgvVysledky.Rows.Clear();
            while (sqlDataReader.Read())
            {
                Console.WriteLine("Name = " + sqlDataReader["praceId"] + sqlDataReader["popisPrace"]);
                Object[] o = new Object[2];
                o[0] = sqlDataReader["praceId"];
                o[1] = sqlDataReader["popisPrace"];
                dgvVysledky.Rows.Add(o);
            }
            sqlDataReader.Close();
            //command.Parameters.Add("@CustomerId", SqlDbType.NChar, 5);
            //command.Parameters["@CustomerId"].Value = customerId;
        }
    }
}
